package com.higgins.eventtracking.client.finance;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.higgins.eventtracking.client.AssetTrackingService;
import com.higgins.eventtracking.client.AssetTrackingServiceAsync;
import com.higgins.eventtracking.client.FinanceTrackingService;
import com.higgins.eventtracking.client.FinanceTrackingServiceAsync;
import com.higgins.eventtracking.client.SecurityAccessDeniedException;
import com.higgins.eventtracking.client.dto.AssetDTO;
import com.higgins.eventtracking.client.dto.TransactionDTO;
import com.higgins.eventtracking.client.rpc.FixedOutgoingService;
import com.higgins.eventtracking.client.rpc.FixedOutgoingServiceAsync;

public class TransactionGenericTable extends GenericTable	{
	private static final Logger log = Logger.getLogger(TransactionGenericTable.class.getName());
	private final FinanceTrackingServiceAsync financeTrackingService = GWT
									.create(FinanceTrackingService.class);
//	private final FixedOutgoingServiceAsync fixedOutgoingService = GWT
//			.create(FixedOutgoingService.class);
	private Integer weekNumber = 0;
	
	public TransactionGenericTable(TransactionGenericColumns columns, Integer weekNumber)	{
		super(columns);
		this.weekNumber = weekNumber;
		retrieveDTOs();
	}
	
	private List<List<Object>> convertDTOtoRowArray(List<TransactionDTO> dtos)	{
		List<List<Object>> rows = new ArrayList<List<Object>>();
		Double total = 0.0;
		Double nonYearlyTotal = 0.0;
		for (TransactionDTO dto:dtos)	{
			List<Object> row = new ArrayList<Object>();
			row.add(dto.getId());
			row.add(dto.getTimestamp());
			row.add(dto.getDescription());
			row.add(dto.getTransactionType());
			row.add(dto.getAmount());
			row.add(dto.getYearlyExpense());
			total = total + dto.getAmount();
			row.add(total.toString());
			if (dto.getYearlyExpense()==null || !dto.getYearlyExpense())	{
				nonYearlyTotal = nonYearlyTotal + dto.getAmount();
			}
			row.add(nonYearlyTotal.toString());
			rows.add(row);
		}
		return rows;
	}
	
	public void updateItemCell(int row)	{
		TransactionDTO dto = new TransactionDTO();
		dto.setId(getId(row));
		dto.setTimestamp(getDate(row, 1));
		dto.setDescription(getText(row, 2));
		dto.setTransactionType(getText(row, 3));
		dto.setAmount(getDouble(row, 4));
		dto.setYearlyExpense(getBoolean(row, 5));
		updateRow(dto);
	}
	
	protected void addObject(int row, int column, Object object)	{
		ensureRowAvailable(row);
		GenericColumn genericColumn = genericColumns.getColumn(column);
		if (column==6 || column==7)	{
			addLabel(row, column, object, genericColumn);
		}
	}		
	
	public void addLabel(int row, int column, Object object, GenericColumn genericColumn)	{
		Label newLabel = new Label();
		newLabel.setText(((String)object));
		setWidget(row, column, newLabel);
		getCellFormatter().addStyleName(row, column, "FlexTable-normCell");
	}	
	
	public void setWeekNumber(Integer weekNumber)	{
		this.weekNumber = weekNumber;
	}
	protected void retrieveDTOs()	{
		try {
			financeTrackingService.retrieveTransactions(weekNumber, true, new AsyncCallback<List<TransactionDTO>>()	{
					@Override
					public void onFailure(Throwable caught) {
						log.info("FixedOutgoingDTO: " + caught.toString());
					}

					@Override
					public void onSuccess(List<TransactionDTO> result) {
						loadData(convertDTOtoRowArray(result));
					}
				});
		} catch (IllegalArgumentException e) {
			log.info("TransactionGenericTable: " + e.toString());
		} catch (SecurityAccessDeniedException e) {
			log.info("TransactionGenericTable: " + e.toString());
		} 
	}	
	private void updateRow(TransactionDTO dto)	{
		System.out.println("updating fixedOutgoingDTO " + dto.getId());
		try {
			financeTrackingService.updateTransaction(dto, new AsyncCallback<String>()	{
					@Override
					public void onFailure(Throwable caught) {
						log.info("TransactionGenericTable: " + caught.toString());
					}

					@Override
					public void onSuccess(String result) {
						setRowIdAfterUpdate(result);
					}
				});
		} catch (IllegalArgumentException e) {
			log.info("TransactionGenericTable: " + e.toString());
		} catch (SecurityAccessDeniedException e) {
			log.info("TransactionGenericTable: " + e.toString());
		} 	
	}
	protected void deleteRow(Long id)	{
		try {
			financeTrackingService.deleteTransaction(id, new AsyncCallback<String>()	{
					@Override
					public void onFailure(Throwable caught) {
						log.info("TransactionGenericTable: " + caught.toString());
					}

					@Override
					public void onSuccess(String result) {
						refresh();
					}
				});
		} catch (IllegalArgumentException e) {
			log.info("TransactionGenericTable: " + e.toString());
		} catch (SecurityAccessDeniedException e) {
			log.info("TransactionGenericTable: " + e.toString());
		} 	

	}
}
